﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Level26EnglishLetters3 : MonoBehaviour
{
    public List<TextButton> textButtonList = new List<TextButton>();
    private string finalStr;
    public GameLevelItem gameLevelItem;
    void Start()
    {
        InitTextButton();
    }

    private void InitTextButton()
    {
        int length = textButtonList.Count;
        for (int i = 0; i < length; i++)
        {
            int index = i;
            textButtonList[i].Init("a", () =>
            {
                TextButtonClick(index);
            });
        }
    }

    private void TextButtonClick(int index)
    {
        if (GameCtrl.Instance.curGameState == GameState.Play)
        {
            int leftIndex = index - 1;
            int rightIndex = index + 1;
            if (leftIndex >= 0)
            {
                TextButton tb = textButtonList[leftIndex];
                tb.text.text = GameCtrl.Instance.GetNextLetter(tb.text.text);
            }
            if (rightIndex < textButtonList.Count)
            {
                TextButton tb = textButtonList[rightIndex];
                tb.text.text = GameCtrl.Instance.GetNextLetter(tb.text.text);
            }
            Judge();
        }
    }

    private void Judge()
    {
        int length = textButtonList.Count;
        finalStr = string.Empty;
        for (int i = 0; i < length; i++)
        {
            finalStr += textButtonList[i].text.text;
        }
        if (finalStr.Equals(UConst.Str_TargetLove))
            gameLevelItem.TriggerResult(true);

    }

}
